import java.awt.Toolkit;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Simple demo that uses java.util.Timer to schedule a task to execute once 5
 * seconds have passed.
 */

public class AITimer extends TimerTask {
	//Toolkit toolkit;

	//Timer timer;
	
	
	/*public AITimer(int seconds) {
		toolkit = Toolkit.getDefaultToolkit();
		timer = new Timer();
		//timer.schedule(new RemindTask(), 3000);
	}*/

		public void run() {
			/*if ((Global.isPVCMode == true)
					&& (Main.getControllerSystem().getPlayerTurn() == 2))
				AITurn();*/
			if(Global.globalCounter%2 == 1)
			{
				if ((Global.isPVCMode == true)
				&& (Main.getControllerSystem().getPlayerTurn() == 2))
			AITurn();
			if(Global.globalCounter == 5000){
				Global.globalCounter = 0;
			}
			}
			Global.globalCounter++;
			//timer.schedule(new RemindTask(), 3000);
			// timer.cancel(); //Not necessary because we call System.exit
			// System.exit(0); //Stops the AWT thread (and everything else)
		}
	public void AITurn() {
		int AIValue;
		int moveValue;
		int tokenLocation_2[][] = { { 830, 130 }, { 880, 130 }, { 830, 180 },
				{ 880, 180 }, { 830, 230 }, { 880, 230 }, { 830, 280 },
				{ 880, 280 }, { 855, 320 } };
		int nodeIndex = 0;
		int startMoveX = 0;
		int startMoveY = 0;
		int endMoveX = 0;
		int endMoveY = 0;
		int m = 0;
		int n = 0;

		String currentPosition;
		String testPosition;

		if(Global.mutex == 0){
			Global.mutex=1;
		if (Main.getControllerSystem().getPlayerTurn() == 2 && Global.millFormed == false) {
			if (Global.token2Played < 9) {
				done: for (int i = 1; i < 8; i++) {
					for (int j = 1; j < 8; j++) {
						AIValue = Main.getModelSystem().readPosition(i, j);
						if (AIValue != 3) {
							if (Main.getControllerSystem().isValidMove(0, 0, i,j)) {
								nodeIndex = Main.getModelSystem()
										.getIndex(i, j);
								endMoveX = Main.getModelSystem().returnNodeX(
										nodeIndex);
								endMoveY = Main.getModelSystem().returnNodeY(
										nodeIndex);
								try {
									Main.getControllerSystem().moveToken(0, 0,
											i, j, 2);
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								// Main.getViewSystem().clearBoard(true);
								Main.getViewSystem()
										.animateToken(
												tokenLocation_2[(Global.token2Played) - 1][0],
												tokenLocation_2[(Global.token2Played) - 1][1],
												endMoveX, endMoveY);
								break done;
							}
						}
					}
				}
			}
			else if(Global.token2Played >= 9){
				done3: for (int i = 1; i < 8; i++) {
					for (int j = 1; j < 8; j++) {
						AIValue = Main.getModelSystem().readPosition(i, j);
						if (AIValue == 2) {
							currentPosition = "" + i + j;
							for (int l = 0; l < 24; l++) {
							if (Main.getControllerSystem().getValidMovesTable()[l][0].equals(currentPosition)) {
								for (int k = 1; k < 5; k++) { // Find desired position
																// in table.
										testPosition = Main.getControllerSystem().getValidMovesTable()[l][k];
										if(testPosition != ""){
										m = Integer.parseInt(testPosition.substring(0,1));
										n = Integer.parseInt(testPosition.substring(1,2));
										}
										moveValue = Main.getModelSystem().readPosition(m, n);
										if (moveValue == 0){
											nodeIndex = Main.getModelSystem()
													.getIndex(m, n);
											endMoveX = Main.getModelSystem().returnNodeX(
													nodeIndex);
											endMoveY = Main.getModelSystem().returnNodeY(
													nodeIndex);
											nodeIndex = Main.getModelSystem().getIndex(i,j);
											startMoveX = Main.getModelSystem().returnNodeX(
													nodeIndex);
											startMoveY = Main.getModelSystem().returnNodeY(
													nodeIndex);
											try {
												Main.getControllerSystem().moveToken(i, j,
														m, n, 2);
											} catch (IOException e1) {
												// TODO Auto-generated catch block
												e1.printStackTrace();
											}
											// Main.getViewSystem().clearBoard(true);
											Main.getViewSystem()
													.animateToken(
															startMoveX,
															startMoveY,
															endMoveX, endMoveY);
											break done3;
										}
								}
						}
					}
				}
			}
		}
			}
		}
		else if (Main.getControllerSystem().getPlayerTurn() == 2
				&& Global.millFormed == true) {
			done2: for (int i = 1; i < 8; i++) {
				for (int j = 1; j < 8; j++) {
					AIValue = Main.getModelSystem().readPosition(i, j);
					if (AIValue == 1) {
						nodeIndex = Main.getModelSystem().getIndex(i, j);
						endMoveX = Main.getModelSystem().returnNodeX(nodeIndex);
						endMoveY = Main.getModelSystem().returnNodeY(nodeIndex);
						// Main.getViewSystem().clearBoard(true);
				    	try {
							Main.getControllerSystem().removeToken(2);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						Main.getViewSystem().AIdeleteToken(endMoveX, endMoveY);
						break done2;
					}
				}
			}
		}
		Global.mutex = 0;
	  }
	}

}